export default class GUIFieldset {
	constructor(folder,legend,parent) {
		this.folder = folder ;
		//上级GUIFieldset对象
		this.parent = parent ;
		this.isFieldset = true ;
		this.container = null ;

		//标题
		this.legend = legend ;
		//是否隐藏状态
		this._hidden = false ;

		this.$fieldset = null ;
		this.domElement = null ;
		this.legendTag = null ;
		this.$fieldsetContent = null ;

		this.group = null ;

		this.init() ;
	}

	init(){
		const fieldset = this.$fieldset = this.domElement = document.createElement("fieldset") ;
		fieldset.classList.add("elem-field") ;

		const legendTag = this.legendTag = document.createElement("legend") ;
		legendTag.classList.add("field-legend") ;
		fieldset.appendChild(legendTag) ;

		const contentTag = this.$fieldsetContent = document.createElement("div") ;
		contentTag.classList.add("field-box") ;
		fieldset.appendChild(contentTag) ;

		if(this.parent == null) this.parent = this.folder.$fieldset ;
		//如果是在某fieldset中，则添加到fieldset中
		//如果是动态生成的，并且当前文件夹在fieleset中，则同样添加
		if(this.parent != null) {
			this.container = this.parent.$fieldsetContent ;
			// this.parent.$fieldsetContent.appendChild( fieldset );
		}
		else
			this.container = this.folder.$children ;

		this.container.appendChild(fieldset) ;

		this.updateDisplay() ;

		return this ;
	}

	/**
	 * 移动到最顶层
	 * @returns {GUIFieldset}
	 */
	toFirst(){
		if(this.container == null) return this ;
		let allFieldsArray = this._findChildFiedset() ;
		//判断数量
		if(allFieldsArray.length <= 1) return this ;
		//已经在第一个了
		if(allFieldsArray[0] == this.$fieldset) return this ;
		this.container.removeChild(this.$fieldset) ;
		this.container.insertBefore(this.$fieldset,allFieldsArray[0]) ;

		return this ;
	}

	_findChildFiedset(){
		let array = [] ;
		if(this.container == null) return array ;
		for(let i=0; i<this.container.children.length; i++){
			let child = this.container.children[i] ;
			if(child.nodeName == "FIELDSET") {
				array.push(child) ;
			}
		}
		return array ;
	}

	setRootStyle(style){
		if(style == null) return this ;
		this.$fieldset.setAttribute("style",style) ;
		return this ;
	}

	addClass(clazz){
		if(clazz == null || clazz == "") return this ;
		this.$fieldset.classList.add(clazz) ;
		return this ;
	}

	removeClass(clazz){
		if(clazz == null || clazz == "") return this ;
		this.$fieldset.classList.remove(clazz) ;
		return this ;
	}

	getControllerContainer(){
		if(this.$fieldsetContent != null) return this.$fieldsetContent ;
		return this.folder.$children ;
	}

	addGroup(group){
		this.group = group ;
		group.addFieldset(this) ;
		return this ;
	}

	setLegend(legend){
		if(legend == null) return this ;
		this.legend = legend ;
		this.legendTag.innerHTML = legend ;
		this.legendTag.classList.toggle("field-legend-label", legend != null && legend != "") ;
		return this ;
	}

	updateDisplay(){
		this.setLegend(this.legend) ;
		return this ;
	}

	show( show = true ){
		this._hidden = !show;

		this.$fieldset.style.display = this._hidden ? 'none' : '';

		return this ;
	}

	disable( disabled = true ) {
		return this ;
	}

	readonly(flag = true){
		return this ;
	}

	destroy(){
		try{
			this.container.removeChild( this.$fieldset );
			// if(this.parent != null)
			// 	this.parent.$fieldsetContent.removeChild( this.$fieldset );
			// else
			// 	this.folder.$children.removeChild( this.$fieldset );
			this.folder.fieldsets.splice( this.folder.fieldsets.indexOf( this ), 1) ;
		}catch(e){}
		return this ;
	}
}
